<?php
    header('content-type:text/html;charset="utf-8') ;

    $host = 'localhost:3306';
    $name = 'root';
    $pwd = 'root';
    $dbname = 'day24';
    $conn = mysqli_connect($host , $name , $pwd , $dbname);

    if(!$conn) {
        die('数据库连接失败');
    }

    $kw = $_GET['kw'];
    $sortType = $_GET['sortType'];


    $page = $_GET['page'] ;   
    $showSize = $_GET['showSize'] ;  
    $type = $_GET['type'] ;
    $sortId = $_GET['sortId'] ;

    $hassort = $_GET['hasSortType'];


    $start = ($page - 1) * $showSize ;


    $obj = [] ;

    if($type){
        if($hassort){
            $sql1 = "select count(*) pageAll from goods_list where goods_title like '%$kw%' order by $hassort $sortType";
            $sql2 = "select * from goods_list where goods_title like '%$kw%' order by $hassort $sortType limit $start , $showSize ";
        }else{
        $sql1 = "select count(*) pageAll from goods_list where goods_title like '%$kw%' and goods_type = '$type' order by $sortId $sortType";
        $sql2 = "select * from goods_list where goods_title like '%$kw%' and goods_type = '$type' order by $sortId $sortType limit $start , $showSize ";
        }
    }else{
        if($hassort){
            $sql1 = "select count(*) pageAll from goods_list where goods_title like '%$kw%' order by $hassort $sortType";
            $sql2 = "select * from goods_list where goods_title like '%$kw%' order by $hassort $sortType limit $start , $showSize ";
        }else{
        $sql1 = "select count(*) pageAll from goods_list where goods_title like '%$kw%' order by $sortId $sortType";
        $sql2 = "select * from goods_list where goods_title like '%$kw%' order by $sortId $sortType limit $start , $showSize ";
        }
    }
    
    // 默认只会转第一条数据
    $res = mysqli_query($conn , $sql1) ;
    $arr = mysqli_fetch_array($res) ;
    $pageAll = $arr['pageAll'] ;
    // var_dump($pageAll);


    $res = mysqli_query($conn , $sql2) ;
    $list = [] ;

    while($data = mysqli_fetch_array($res)) {
        $temp = [] ;
        $temp['id'] = $data['goods_id'] ;
        $temp['name'] = $data['goods_name'] ;
        $temp['title'] = $data['goods_title'] ;
        $temp['des'] = $data['goods_des'] ;
        $temp['img'] = $data['goods_img'] ;
        $temp['price'] = $data['goods_price'] ;
        $temp['type'] = $data['goods_type'] ;
        array_push($list , $temp) ;
    }

    if($list) {
        $obj['status'] = true ;
        $obj['msg'] = '数据请求成功';
        $obj['data'] = $list ;
        $obj['pageAll'] = $pageAll ;
    } else {
        $obj['status'] = false ;
        $obj['msg'] = '没有符合的数据';
        $obj['data'] = $list ;
    }

    echo(json_encode($obj , JSON_UNESCAPED_UNICODE));
?>